package com.mg.g2048.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mg.g2048.R;

public class GameAdapter extends BaseAdapter {
	Context context = null;
	float width = 0;
	int[][] gameData = null;
	int[][] gameChangeData = null;
	
	public GameAdapter(Context context, float width, int[][] gameData, int[][] gameChangeData){
		this.context = context;
		this.gameData = gameData;
		this.gameChangeData = gameChangeData;
		this.width = width;
	}
	@Override
	public int getCount() {
		if(gameData == null)
			return 0;
		else
			return gameData.length * 4;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);  
		
		int x = arg0 / 4;
		int y = arg0 % 4;
		int listData = gameData[x][y];
		View item = inflater.inflate(R.layout.item, parent, false); 
		TextView title = (TextView) item.findViewById(R.id.title);
		
		LayoutParams params = item.getLayoutParams();
		params.height = (int) width;
		item.setLayoutParams(params);
		if(listData > 9999)
			title.setTextSize(18);
		else if(listData > 999 && listData < 9999)
			title.setTextSize(20);
		else if(listData > 99 && listData < 999)
			title.setTextSize(20);
		if(gameChangeData[x][y] != 0){
			Animation animation = AnimationUtils.loadAnimation(context, R.anim.min_max); 
			item.startAnimation(animation);
		}
		switch(listData){
			case 0:
				item.setVisibility(View.GONE);
				break;
			case 2:
				title.setBackgroundResource(R.drawable.bg1);
				break;
			case 4:title.setBackgroundResource(R.drawable.bg2);
				break;
			case 8:title.setBackgroundResource(R.drawable.bg3);
				break;
			case 16:title.setBackgroundResource(R.drawable.bg4);
				break;
			case 32:title.setBackgroundResource(R.drawable.bg5);
				break;
			case 64:title.setBackgroundResource(R.drawable.bg6);
				break;
			case 128:title.setBackgroundResource(R.drawable.bg7);
				break;
			case 256:title.setBackgroundResource(R.drawable.bg8);
				break;
			case 512:title.setBackgroundResource(R.drawable.bg1);
				break;
			case 1024:title.setBackgroundResource(R.drawable.bg2);
				break;
			case 2048:title.setBackgroundResource(R.drawable.bg3);
				break;
			default:title.setBackgroundResource(R.drawable.bg4);
				break;
		}
		if(listData != 0)
			title.setText(String.valueOf(listData));
		item.setId(arg0);
		
		return item;
	}

}
